package com.jiangxjsy.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.jiangxjsy.admin.service.AdminRoleService;
import com.jiangxjsy.core.wrap.Result;
import com.jiangxjsy.core.wrap.ResultBuilder;
import com.jiangxjsy.model.admin.entity.AdminRole;
import com.jiangxjsy.model.admin.vo.AdminRoleVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 17:21
 */

@RestController
@RequestMapping("/adminRole")
public class AdminRoleController {
    @Resource
    private AdminRoleService adminRoleService;

    /**
     * 获取未授权的菜单
     *
     * @param userId 用户 id
     * @return 菜单列表 Result
     */
    @PostMapping("/queryNoAuthMenu")
    public Result<List<String>> queryNoAuthMenu(@RequestParam("userId") @NotNull Long userId) {
        return ResultBuilder.ok(adminRoleService.queryNoAuthMenu(userId));
    }

    /**
     * 角色权限
     *
     * @return Result
     */
    @PostMapping("/auth")
    public Result<JSONObject> auth(HttpServletRequest request, HttpServletResponse response) {
        JSONObject object = adminRoleService.auth(request, response);
        return ResultBuilder.ok(object);
    }

    /**
     * 获取角色类型列表
     *
     * @return Result
     */
    @PostMapping("/getRoleTypeList")
    public Result<List<Map<String, Object>>> getRoleTypeList() {
        return ResultBuilder.ok(adminRoleService.getRoleTypeList());
    }

    /**
     * 获取角色列表
     *
     * @return Result
     */
    @PostMapping("/getRoleList")
    public Result<List<AdminRoleVO>> getRoleList(HttpServletRequest request, HttpServletResponse response){
        return ResultBuilder.ok(adminRoleService.getRoleList(request, response));
    }

    /**
     * 通过角色类型查询角色
     *
     * @param type 角色类型
     * @return Result
     */
    @PostMapping("/queryRoleByRoleType")
    public Result<List<Long>> queryRoleByRoleType(@RequestParam("type") Integer type) {
        List<AdminRole> recordList = adminRoleService.queryRoleByRoleType(type);
        return ResultBuilder.ok(recordList.stream().map(AdminRole::getRoleId).collect(Collectors.toList()));
    }
}
